package com.pearlsoft.webapp.rms.vdcmgr;

import java.util.Map;
import java.util.logging.Level;

import com.pearlsoft.rms.VcloudManager;
import com.pearlsoft.webapp.core.annotation.DataSetHandlerComponent;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Result;
import com.pearlsoft.webapp.core.web.basehandler.AbsDataSetHandler;
import com.vmware.vcloud.sdk.Organization;
import com.vmware.vcloud.sdk.Vdc;
import com.vmware.vcloud.sdk.admin.AdminOrganization;
import com.vmware.vcloud.sdk.admin.AdminVdc;

@DataSetHandlerComponent(name = "VDCMgrDatasetHandler")
public class VDCMgrDatasetHandler extends AbsDataSetHandler {
	
	public void handler(DataSet ds, Result result) throws Exception {

	}

	public void save(final DataSet ds, Result result) throws Exception {
		
	}
	
	
	public void delete(final DataSet ds, Result result) throws Exception {
		
	}
	
	public void deleteAll(final DataSet ds, Result result) throws Exception {
		
	}
	/*
	 * 设置启用
	 */
	public void setEnabled(final DataSet ds, Result result) throws Exception {
		String vdcName = (String)ds.getConditions().get("VDC_Name");
		VcloudManager vcm = this.getBean("vcloudManager", VcloudManager.class);
		vcm.setLogLevel(Level.OFF);
		//vcm.login("https://192.168.0.7", "administrator@test", "abc123..");
		Map<String, AdminOrganization> orgs = vcm.getAdminOrgMap();
		AdminOrganization org = orgs.get(System.getProperty("ORG_NAME"));
		Map<String, AdminVdc> vdcs = vcm.getAdminVdcMap(org);
		AdminVdc vdc = vdcs.get(vdcName);
		if(!vdc.getResource().isIsEnabled()){
			vdc.getResource().setIsEnabled(true);
			vdc.enable();
		}
	}
	
	/*
	 * 设置禁用
	 */
	public void setDisabled(final DataSet ds, Result result) throws Exception {
		String vdcName = (String)ds.getConditions().get("VDC_Name");
		VcloudManager vcm = this.getBean("vcloudManager", VcloudManager.class);
		vcm.setLogLevel(Level.OFF);
		//vcm.login("https://192.168.0.7", "administrator@test", "abc123..");
		Map<String, AdminOrganization> orgs = vcm.getAdminOrgMap();
		AdminOrganization org = orgs.get(System.getProperty("ORG_NAME"));
		Map<String, AdminVdc> vdcs = vcm.getAdminVdcMap(org);
		AdminVdc vdc = vdcs.get(vdcName);
		if(vdc.getResource().isIsEnabled()){
			vdc.getResource().setIsEnabled(true);
			vdc.disable();
		}
	}
	
	

}
